<?php
class HighLowGame {
    var $myValue;
    var $opponentValue;
    var $matchId;

    // 0 = biggest positive difference
    // 1 = lowest positive difference
    // 2 = highest total
    // 3 = lowest total
    // 4 = my highest
    // 5 = my lowest
    var $method = 0; 

    function HighLowGame($method) {
        $this->method = $method;
        switch ($method) {
            case 0 :
                $this->myValue = 0;
                $this->opponentValue = 100;
                break;
            case 1 :
                $this->myValue = 100;
                $this->opponentValue = 0;
                break;
            case 2 :
            case 4 :
                $this->myValue = -1;
                $this->opponentValue = -1;
                break;
            case 3 :
            case 5 :
                $this->myValue = 100;
                $this->opponentValue = 100;
                break;
        }
    }

    function updateValue(& $myValue, & $opponentValue, & $matchId) {
        if (!isset ($this->myValue)) {
            $this->myValue = $myValue;
            $this->opponentValue = $opponentValue;
            return;
        }

        $newDiff = $myValue - $opponentValue;
        $oldDiff = $this->myValue - $this->opponentValue;

        $newTotal = $myValue + $opponentValue;
        $oldTotal = $this->myValue + $this->opponentValue;

        if ($this->method == 0) {
            $this->doUpdate($newDiff, $oldDiff, $newTotal, $oldTotal, $myValue, $opponentValue, $matchId);
        } else
            if ($this->method == 1) {
                $this->doUpdate($oldDiff, $newDiff, $oldTotal, $newTotal, $myValue, $opponentValue, $matchId);
            } else
                if ($this->method == 2) {
                    $this->doUpdate($newTotal, $oldTotal, $newDiff, $oldDiff, $myValue, $opponentValue, $matchId);
                } else
                    if ($this->method == 3) {
                        $this->doUpdate($oldTotal, $newTotal, $oldDiff, $newDiff, $myValue, $opponentValue, $matchId);
                    } else
                        if ($this->method == 4) {
                            if ($myValue > $this->myValue) {
                                $this->modifyValues($myValue, $opponentValue, $matchId);
                            } else
                                if ($myValue == $this->myValue and $this->opponentValue > $opponentValue) {
                                    $this->modifyValues($myValue, $opponentValue, $matchId);
                                }
                        } else
                            if ($this->method == 5) {
                                if ($myValue < $this->myValue) {
                                    $this->modifyValues($myValue, $opponentValue, $matchId);
                                } else
                                    if ($myValue == $this->myValue and $this->opponentValue > $opponentValue) {
                                        $this->modifyValues($myValue, $opponentValue, $matchId);
                                    }
                            }
    }

    function doUpdate(& $total1, & $total2, & $total3, & $total4, & $myValue, & $opponentValue, & $matchId) {
        if ($total1 > $total2 or ($total1 == $total2 and $total3 > $total4)) {
            $this->modifyValues($myValue, $opponentValue, $matchId);
        }
    }

    function modifyValues(& $myValue, & $opponentValue, & $matchId) {
        $this->myValue = $myValue;
        $this->opponentValue = $opponentValue;
        $this->matchId = $matchId;
    }

}
?>